<?php
// +----------------------------------------------------------------------
// | HkCms 前台插件基类
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace app\addons\util;

use common\controller\Base;

class AddonsBase extends Base {

    //插件标识
    public $addonName = NULL;
    //插件基本信息
    protected $addonInfo = NULL;
    //插件路径
    protected $addonPath = NULL;

    public function _initialize() {
        parent::_initialize();
        $this->addonName = request()->controller();
        $addons = hk_cache('Addons');
        $this->addonInfo = !empty($addons[strtolower($this->addonName)]) ? $addons[strtolower($this->addonName)]:'';
        if (empty($this->addonInfo)) {
            $this->error('该插件没有安装或者已经被禁用！');
        }
        $this->addonPath = model('addons/Addons')->getAddonsPath() . strtolower($this->addonName) . '/';
        //插件配置文件
        $this->configFile = $this->addonPath . 'config.php';
    }

    /**
     * 渲染内容输出
     * @access public
     * @param string $templateFile 内容
     * @param array  $vars    模板输出变量
     * @param array  $replace 替换内容
     * @param array  $config     模板参数
     * @return mixed
     */
    protected function display($templateFile = '', $vars = [], $replace = [], $config = []) {
        return $this->view->fetch(parseAddonTemplateFile($templateFile, $this->addonPath), $vars, $replace, $config);
    }

    /**
     * 获取插件配置
     * @staticvar array $_config
     * @return type
     */
    final public function getAddonConfig() {
        return $this->addonInfo['config'];
    }

}
